*/
class PostgresField implements Field {
- private $name, $tablename, $type, $nullable, $max_length, $deferred, $deferrable, $conname;
+ private $name, $tablename, $type, $nullable, $max_length, $deferred, $deferrable, $conname,
+ $has_default, $default;
/**
* @param $db DatabaseBase
JOIN pg_attribute a ON (a.attrelid = c.oid)
JOIN pg_type t ON (t.oid = a.atttypid)
LEFT JOIN pg_constraint o ON (o.conrelid = c.oid AND a.attnum = ANY(o.conkey) AND o.contype = 'f')
+LEFT JOIN pg_attrdef d on c.oid=d.adrelid and a.attnum=d.adnum
WHERE relkind = 'r'
AND nspname=%s
AND relname=%s
$n->deferrable = ( $row->deferrable == 't' );
$n->deferred = ( $row->deferred == 't' );
$n->conname = $row->conname;
+ $n->has_default = ( $row->atthasdef === 't' );
+ $n->default = $row->adsrc;
return $n;
}
function conname() {
return $this->conname;
}
+ /**
+ * @since 1.19
+ */
+ function defaultValue() {
+ if( $this->has_default ) {
+ return $this->default;
+ } else {
+ return false;
+ }
+ }
}
* Used to debug transaction processing
* Only used if $wgDebugDBTransactions is true
*
- * @since 1.20
+ * @since 1.19
* @ingroup Database
*/
class PostgresTransactionState {
}
protected function doQuery( $sql ) {
- global $wgDebugDBTransactions;
if ( function_exists( 'mb_convert_encoding' ) ) {
$sql = mb_convert_encoding( $sql, 'UTF-8' );
}
return false;
}
+ /**
+ * Returns is of attributes used in index
+ *
+ * @since 1.19
+ * @return Array
+ */
+ function indexAttributes ( $index, $schema = false ) {
+ if ( $schema === false )
+ $schema = $this->getCoreSchema();
+ /*
+ * A subquery would be not needed if we didn't care about the order
+ * of attributes, but we do
+ */
+ $sql = <<<__INDEXATTR__
+
+ SELECT opcname,
+ attname,
+ i.indoption[s.g] as option,
+ pg_am.amname
+ FROM
+ (SELECT generate_subscripts(isub.indkey, 1) AS g
+ FROM
+ pg_index isub
+ JOIN pg_class cis
+ ON cis.oid=isub.indexrelid
+ JOIN pg_namespace ns
+ ON cis.relnamespace = ns.oid
+ WHERE cis.relname='$index' AND ns.nspname='$schema') AS s,
+ pg_attribute,
+ pg_opclass opcls,
+ pg_am,
+ pg_class ci
+ JOIN pg_index i
+ ON ci.oid=i.indexrelid
+ JOIN pg_class ct
+ ON ct.oid = i.indrelid
+ JOIN pg_namespace n
+ ON ci.relnamespace = n.oid
+ WHERE
+ ci.relname='$index' AND n.nspname='$schema'
+ AND attrelid = ct.oid
+ AND i.indkey[s.g] = attnum
+ AND i.indclass[s.g] = opcls.oid
+ AND pg_am.oid = opcls.opcmethod
+__INDEXATTR__;
+ $res = $this->query($sql, __METHOD__);
+ $a = array();
+ if ( $res ) {
+ foreach ( $res as $row ) {
+ $a[] = array(
+ $row->attname,
+ $row->opcname,
+ $row->amname,
+ $row->option);
+ }
+ } else {
+ return null;
+ }
+ return $a;
+ }
+
+
function indexUnique( $table, $index, $fname = 'DatabasePostgres::indexUnique' ) {
$sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'".
" AND indexdef LIKE 'CREATE UNIQUE%(" .
# Replace reserved words with better ones
switch( $name ) {
case 'user':
- return 'mwuser';
+ return $this->realTableName( 'mwuser', $format );
case 'text':
- return 'pagecontent';
+ return $this->realTableName( 'pagecontent', $format );
default:
- return parent::tableName( $name, $format );
+ return $this->realTableName( $name, $format );
}
}
+ /* Don't cheat on installer */
+ function realTableName( $name, $format = 'quoted' ) {
+ return parent::tableName( $name, $format );
+ }
+
/**
* Return the next in a sequence, save the value for retrieval via insertId()
* @return null
*
* This should really be handled by PHP PostgreSQL module
*
- * @since 1.20
+ * @since 1.19
* @param $text string: postgreql array returned in a text form like {a,b}
* @param $output string
* @param $limit int
* Return current schema (executes SELECT current_schema())
* Needs transaction
*
- * @since 1.20
+ * @since 1.19
* @return string return default schema for the current session
*/
function getCurrentSchema() {
*
* @seealso getSearchPath()
* @seealso setSearchPath()
- * @since 1.20
+ * @since 1.19
* @return array list of actual schemas for the current sesson
*/
function getSchemas() {
* (like "$user").
* Needs transaction
*
- * @since 1.20
+ * @since 1.19
* @return array how to search for table names schemas for the current user
*/
function getSearchPath() {
/**
* Update search_path, values should already be sanitized
* Values may contain magic keywords like "$user"
- * @since 1.20
+ * @since 1.19
*
* @param $search_path array list of schemas to be searched by default
*/
*
* This will be also called by the installer after the schema is created
*
- * @since 1.20
+ * @since 1.19
* @param desired_schema string
*/
function determineCoreSchema( $desired_schema ) {
/**
* Return schema name fore core MediaWiki tables
*
- * @since 1.20
+ * @since 1.19
* @return string core schema name
*/
function getCoreSchema() {
if ( !$schema ) {
$schema = $this->getCoreSchema();
}
- $table = $this->tableName( $table, 'raw' );
+ $table = $this->realTableName( $table, 'raw' );
$etable = $this->addQuotes( $table );
$eschema = $this->addQuotes( $schema );
$SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
array( 'renameIndex', 'mwuser', 'user_user_name_key', 'mwuser_user_name_key' ),
array( 'renameIndex', 'pagecontent','text_pkey', 'pagecontent_pkey' ),
- # new sequences
- array( 'addSequence', 'logging_log_id_seq' ),
- array( 'addSequence', 'page_restrictions_pr_id_seq' ),
-
# renamed sequences
array( 'renameSequence', 'ipblocks_ipb_id_val', 'ipblocks_ipb_id_seq' ),
array( 'renameSequence', 'rev_rev_id_val', 'revision_rev_id_seq' ),
array( 'addTable', 'uploadstash', 'patch-uploadstash.sql' ),
array( 'addTable', 'user_former_groups','patch-user_former_groups.sql' ),
array( 'addTable', 'config', 'patch-config.sql' ),
- array( 'addTable', 'external_user','patch-external_user.sql' ),
+ array( 'addTable', 'external_user', 'patch-external_user.sql' ),
# Needed before new field
array( 'convertArchive2' ),
array( 'changeField', 'image', 'img_size', 'integer', '' ),
array( 'changeField', 'image', 'img_width', 'integer', '' ),
array( 'changeField', 'image', 'img_height', 'integer', '' ),
- array( 'changeField', 'interwiki', 'iw_local', 'smallint', 'iw_local::smallint DEFAULT 0' ),
+ array( 'changeField', 'interwiki', 'iw_local', 'smallint', 'iw_local::smallint' ),
array( 'changeField', 'interwiki', 'iw_trans', 'smallint', 'iw_trans::smallint DEFAULT 0' ),
array( 'changeField', 'ipblocks', 'ipb_auto', 'smallint', 'ipb_auto::smallint DEFAULT 0' ),
array( 'changeField', 'ipblocks', 'ipb_anon_only', 'smallint', "CASE WHEN ipb_anon_only=' ' THEN 0 ELSE ipb_anon_only::smallint END DEFAULT 0" ),